package com.dycong.common.threadUtil;

import java.util.function.*;

/**
 * Created by dycong on 16-2-22.
 */
public class TimerUtil {


    public static long runningMillionSeconds(Runnable method, long times) {
        Supplier<?> supplier = () -> {
            method.run();
            return null;
        };
        return runningMillionSeconds(supplier, times);
    }


    public static long runningMillionSeconds(Supplier<?> method, long times) {
        Function<?, ?> function = a -> method.get();
        return runningMillionSeconds(function, null, times);
    }


    public static <T> long runningMillionSeconds(Consumer<T> method, T param, long times) {
        Function<T, Object> function = (a) -> {
            method.accept(a);
            return null;
        };
        return runningMillionSeconds(function, param, times);
    }


    public static long runningMillionSeconds(IntConsumer method, int param, long times){
        Function<Integer, Object> function = i -> {
            method.accept(i);
            return null;
        };
        return runningMillionSeconds(function, param, times);
    }


    public static <T> long runningMillionSeconds(Function<T, ?> method, T param, long times) {
        BiFunction<T, ?, ?> biFunction = (a, b) -> method.apply(param);
        return runningMillionSeconds(biFunction, param, null, times);
    }

    public static <T, U> long runningMillionSeconds(BiFunction<T, U, ?> method, T param, U param2, long times) {
        long start = System.currentTimeMillis();
        for (long i = 0; i < times; i++) {
            method.apply(param, param2);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }


    public static long runningMillionSeconds(IntBinaryOperator method, int param, int param2, long times) {
        long start = System.currentTimeMillis();
        for (long i = 0; i < times; i++) {
            method.applyAsInt(param, param2);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }


    public static long runningMillionSeconds(LongBinaryOperator method, long param, long param2, long times) {
        long start = System.currentTimeMillis();
        for (long i = 0; i < times; i++) {
            method.applyAsLong(param, param2);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

}
